//
//  LRVisibleView.swift
//  LRWeibo
//
//  Created by 雷路荣 on 15/12/15.
//  Copyright © 2015年 leilurong. All rights reserved.
//

import UIKit
// 1.懒加载所有控件
// 2.添加控件
// 3.设置位置

/**
*  定义代理协议  需要继承NSObjectProtocol
*/
protocol LRVisitorViewDelegate : NSObjectProtocol {
    //点击注册按钮
    func visitorViewDidRegisterButton()
    //点击登录按钮
    func visitorViewDidLoginButton()
}

class LRVisitorView: UIView {
    //添加代理属性默认是strong要设置weak，代理可能要可能没有所以要添加 " ？”,否则报错
    weak var delegate: LRVisitorViewDelegate?
    
    //自动提示弹出该系统方法   // view必须能从从xib/storyboard
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        //加载控件
        prepareUI()
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
    }
    
//-------------------------------2.添加控件及设置frame--------------------------
    private func prepareUI() {
        //1 添加子控件
        addSubview(iconImageView)
        addSubview(coverImageView)//添加遮盖视图
        addSubview(houseImageView)//小房子
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //2 Autoresizing影响AutoLayout的约束
        iconImageView.translatesAutoresizingMaskIntoConstraints = false
        houseImageView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        coverImageView.translatesAutoresizingMaskIntoConstraints = false
        //3 设置约束
        //3.1 设置转轮的约束
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -68))
        //3.2 设置房子的约束
        addConstraint(NSLayoutConstraint(item: houseImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        //3.3 设置文本标签的约束
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.BottomMargin, multiplier: 1, constant: 18))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 255))
        //3.4 设置注册按钮的约束
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.LeadingMargin, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1, constant: 0))//设置左边与标签对齐
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.BottomMargin, multiplier: 1, constant: 28))//设置与标签距离
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 98))
        //3.5 设置登录按钮的约束
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.TrailingMargin, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.TrailingMargin, multiplier: 1, constant: 0))//设置右边与标签右对齐
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        //3.6 设置遮盖视图约束
        addConstraint(NSLayoutConstraint(item: coverImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverImageView, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.TopMargin, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }
    
    //让外界来设置访客视图信息的方法
    func setupVisitorViewInfo(imageName:String, message:String){
        
        houseImageView.hidden = true
        iconImageView.image = UIImage(named: imageName)
        messageLabel.text = message
        //将遮盖视图移到最底部
        sendSubviewToBack(coverImageView)
        //coverImageView.hidden = true//或者隐藏
    }
    // 开启动画
    func startRotationAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        // 要加上这句,不然切换到其他tabBar或者退出到桌面在切回来动画就停止了
        // 当不可见的时候,系统会认为动画完成,在完成的时候不要移除动画
        animation.removedOnCompletion = false
        //给图层添加核心动画
        iconImageView.layer.addAnimation(animation, forKey: "homeRotationAnimation")
    }
    //暂停动画
    func pauseAnimation() {
        //获取当前暂停时间
        let pauseTime = iconImageView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        //设置速度为0
        iconImageView.layer.speed = 0
        //设置动画偏移时间,记录暂停时间
        iconImageView.layer.timeOffset = pauseTime
    }
    //恢复动画
    func resumeAnimation() {
        //获取暂停时间
        let pauseTime = iconImageView.layer.timeOffset
        //设置动画速度
        iconImageView.layer.speed = 1
        //重置偏移时间为0
        iconImageView.layer.timeOffset = 0
        //重置开始时间为0
        iconImageView.layer.beginTime = 0
        //计算开始时间
        let timeSincePause = iconImageView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        //设置开始时间，开始动画
        iconImageView.layer.beginTime = timeSincePause
    }
    
//-------------------------------1.懒加载----------------------------
    //1 转轮
    private lazy var iconImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //2 小房子
    private lazy var houseImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //3 标签
    private lazy var messageLabel:UILabel = {
        let messageLabel = UILabel()
        messageLabel.text = "关注一些人，看看这里又什么惊喜"
        messageLabel.textColor = UIColor.darkGrayColor()
        messageLabel.numberOfLines = 0
        //设置居中
        messageLabel.textAlignment = NSTextAlignment.Center
        messageLabel.sizeToFit()
        return messageLabel
    }()
    //4 注册按钮
    private lazy var registerButton:UIButton = {
        let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    //5 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    //6 遮罩视图, 不要写maskView, 因为View有一个maskView属性
    private lazy var coverImageView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return imageView
    }()
    //view是不能modal出来控制器, 只有控制器才能modal出来
    //注册按钮点击事件 系统会将swift语言前面添加@objc转成oc语言执行 private修饰后不会自动添加@objc，这里要自己添加
    @objc private func registerButtonClick() {
//        if ((delegate?.respondsToSelector("visitorViewDidRegisterButton") ) != nil){
//            delegate?.visitorViewDidRegisterButton()
//        }
        // delegate有值就调用,没有就不调用，不用像上面那么些了
        delegate?.visitorViewDidRegisterButton()
    }
    //登录按钮点击事件
    @objc private func loginButtonClick() {
         delegate?.visitorViewDidLoginButton()
    }
}
